package com.ddpt.permission.dubbo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lh.center.common.enums.MerchantServiceType;
import com.lh.center.common.exception.CommonException;
import com.lh.center.common.exception.ErrorEnum;
import com.lh.center.common.util.BeanTransferUtils;
import com.lh.center.permission.api.dubbo.DubboServiceModuleService;
import com.lh.center.permission.api.entity.ServiceModuleEntity;
import com.lh.center.permission.api.response.ServiceModuleResponse;
import com.lh.center.permission.service.ServiceModuleService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@DubboService
public class DubboServiceModuleServiceImpl implements DubboServiceModuleService {

    @Autowired
    private ServiceModuleService serviceModuleService;

    @Override
    public void saveServiceModule(ServiceModuleEntity moduleEntity) {
        String code = moduleEntity.getCode(),name = moduleEntity.getName();
        if (StringUtils.isAnyBlank(code,name,moduleEntity.getIcon(),moduleEntity.getDescription())) throw new CommonException(ErrorEnum.CRITICAL_PARAM_DEFICIENCY);
        List<ServiceModuleResponse> responseList = queryModuleList(true);
        boolean codeExist = false;
        for (ServiceModuleResponse response : responseList) {
            if (response.getCode().equals(code)) {
                codeExist = true;
                if (StringUtils.isBlank(moduleEntity.getStatus())){
                    moduleEntity.setStatus(response.getStatus());
                }
            } else if(response.getName().equals(name)) {
                throw new CommonException("应用名称已存在，请重新输入");
            }
        }
        if (!codeExist) {
            throw new CommonException("应用不存在！");
        }
        serviceModuleService.saveOrUpdate(moduleEntity);
    }

    @Override
    public List<ServiceModuleResponse> queryModuleList(boolean includeMerchant) {
        LambdaQueryWrapper<ServiceModuleEntity> wrapper = Wrappers.lambdaQuery(ServiceModuleEntity.class).ne(!includeMerchant, ServiceModuleEntity::getCode, MerchantServiceType.MERCHANT.getCode());
        List<ServiceModuleEntity> moduleList = serviceModuleService.list(wrapper);
        List<ServiceModuleResponse> responseList = BeanTransferUtils.transferCollection(moduleList, ServiceModuleResponse.class);
        Set<String> codeSet = responseList.stream().map(ServiceModuleResponse::getCode).collect(Collectors.toSet());
        List<MerchantServiceType> enums = MerchantServiceType.getAll(includeMerchant);
        for (MerchantServiceType cur : enums) {
            if (!codeSet.contains(cur.getCode())) {
                responseList.add(BeanTransferUtils.transferBean(cur,ServiceModuleResponse.class));
            }
        }
        return responseList;
    }

    @Override
    public List<ServiceModuleResponse> querySimpleModuleList(boolean includeMerchant) {
        LambdaQueryWrapper<ServiceModuleEntity> wrapper = Wrappers.lambdaQuery(ServiceModuleEntity.class)
                .select(ServiceModuleEntity::getCode,ServiceModuleEntity::getName)
                .ne(!includeMerchant, ServiceModuleEntity::getCode, MerchantServiceType.MERCHANT.getCode());
        List<ServiceModuleEntity> moduleList = serviceModuleService.list(wrapper);
        List<ServiceModuleResponse> responseList = BeanTransferUtils.transferCollection(moduleList, ServiceModuleResponse.class);
        Set<String> codeSet = responseList.stream().map(ServiceModuleResponse::getCode).collect(Collectors.toSet());
        List<MerchantServiceType> enums = MerchantServiceType.getAll(includeMerchant);
        ServiceModuleResponse curResp;
        for (MerchantServiceType cur : enums) {
            if (!codeSet.contains(cur.getCode())) {
                curResp = new ServiceModuleResponse();
                curResp.setCode(cur.getCode());
                curResp.setName(cur.getName());
                responseList.add(curResp);
            }
        }
        return responseList;
    }
}
